package com.zst.common.validate;

import java.util.function.Function;

/**
 * 优化各类库里面提供的Assert工具无法自定义要抛出的异常类型的问题而创建的Assert工具类
 */
public class Assert {
    public static void isTrue(boolean expression, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (!expression) {
            throw exceptionConstructor.apply(message);
        }
    }

    public static void isFalse(boolean expression, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (expression) {
            throw exceptionConstructor.apply(message);
        }
    }

    public static void notNull(Object arg, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (arg == null) {
            throw exceptionConstructor.apply(message);
        }
    }

    public static void isNull(Object arg, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (arg != null) {
            throw exceptionConstructor.apply(message);
        }
    }

    public static void isEqual(Object arg1, Object arg2, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (arg1 == null || arg2 == null
                || !arg1.getClass().equals(arg2.getClass())
                || !arg1.equals(arg2)) {
            throw exceptionConstructor.apply(message);
        }
    }

    public static void notEmpty(String arg, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (arg == null || arg.trim().length() == 0) {
            throw exceptionConstructor.apply(message);
        }
    }

    public static void isEmpty(String arg, String message, Function<String, RuntimeException> exceptionConstructor) {
        if (arg != null && arg.trim().length() != 0) {
            throw exceptionConstructor.apply(message);
        }
    }
}
